//
//  PriceTendView.swift
//  E-CycleForShop
//
//  Created by enghou on 17/4/21.
//  Copyright © 2017年 xyxorigation. All rights reserved.
//

import UIKit

class PriceTendView: UIView {
    private let border = 3
    var prices : Array<Int> = []{
        didSet{
//            prices.sort()//排个序
        }
    }//forefore fore current
    var months : Array<String> = []
    
    var radius : CGFloat = 8 * YXHeightScale
    private var totalX = 0//纪录横向一共有多少个竖线
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.clear
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //mesh
    func caculateCoordinate(x : CGFloat,y : CGFloat)->CGPoint{
        let realX = x * self.frame.size.height / 10
        let realY = y * self.frame.size.height / 10
        return CGPoint.init(x: realX, y: realY)
    }
    
    override func draw(_ rect: CGRect) {
        super.draw(rect)
        if let context = UIGraphicsGetCurrentContext() {
            //draw mesh
            context.setLineWidth(YXWidthScale)
            context.setStrokeColor(UIColor.lightGray.cgColor)
            let path1 = CGMutablePath.init()
            let perHeigh = rect.size.height / 10.0
            for i in 1 ..< 10 {
                let y = perHeigh * CGFloat(i)
                path1.move(to: CGPoint.init(x: 0, y: y))
                path1.addLine(to: CGPoint.init(x: rect.width, y: y))
            }
            
            var target = perHeigh
            totalX = 0
            let text = "05月"
            let size = text.StringSize(font: UIFont.systemFont(ofSize: 24 * YXWidthScale))
            
            while target <= rect.width {
                path1.move(to: CGPoint.init(x: target, y: 0))
                path1.addLine(to: CGPoint.init(x: target, y: rect.size.height - size.height))
                target += perHeigh
                totalX += 1
            }
            
            context.addPath(path1)
            context.strokePath()
            context.setStrokeColor(UIColor.colorWithHexString(hex: "3cc282").cgColor)
            let deltPrice = prices[2] - prices[0]//总价格差
            let rectPerPrice = CGFloat(deltPrice) / 5.0//每个方格代表的价格差
            let firstPoint = caculateCoordinate(x: CGFloat(border), y: 2)
            let path2 = CGMutablePath.init()
            path2.addArc(center: firstPoint, radius: radius, startAngle: 0, endAngle: CGFloat(Double.pi) * CGFloat(2), clockwise: true)
            context.addPath(path2)
            context.strokePath()
            
            let path3 = CGMutablePath.init()
            let secondToFirst = CGFloat(prices[1]) - CGFloat(prices[0])
            var deltY : CGFloat = 0.0
            if rectPerPrice != 0 {
                deltY = secondToFirst / rectPerPrice//得到第二个图形的y坐标
            }
            let secondPoint = caculateCoordinate(x: CGFloat(totalX) / 2, y: 2 + deltY)
            path3.addArc(center: secondPoint, radius: radius, startAngle: 0, endAngle: CGFloat(Double.pi) * CGFloat(2), clockwise: true)
            context.addPath(path3)
            context.strokePath()
            let path4 = CGMutablePath.init()
            let thirdPoint = caculateCoordinate(x: CGFloat(totalX) - CGFloat(border), y: 7)
            path4.addArc(center: thirdPoint, radius: radius , startAngle: 0, endAngle: CGFloat(Double.pi) * CGFloat(2), clockwise: true)
            context.addPath(path4)
            context.setFillColor(UIColor.black.cgColor)
            context.strokePath()
            
            let path6 = CGMutablePath.init()
            path6.addArc(center: thirdPoint, radius: (radius - 2 * YXHeightScale), startAngle: 0, endAngle: CGFloat(Double.pi) * CGFloat(2), clockwise: true)
            context.addPath(path6)
            context.fillPath()
            
            let tan1 = (secondPoint.y - firstPoint.y) / (secondPoint.x - firstPoint.x)
            let angle1 = atanf(Float(tan1))
            let sin1 = sin(angle1)
            let cos1 = cos(angle1)
            let point1 = CGPoint.init(x: firstPoint.x + radius * CGFloat(cos1), y: firstPoint.y + radius * CGFloat(sin1))
            let point2 = CGPoint.init(x: secondPoint.x - radius * CGFloat(cos1), y: secondPoint.y - radius * CGFloat(sin1))
            
            let tan2 = (thirdPoint.y - secondPoint.y) / (thirdPoint.x - secondPoint.x)
            let angle2 = atanf(Float(tan2))
            let sin2 = sin(angle2)
            let cos2 = cos(angle2)
            let point3 = CGPoint.init(x: secondPoint.x + radius * CGFloat(cos2), y: secondPoint.y + radius * CGFloat(sin2))
            
            let path5 = CGMutablePath.init()
            path5.move(to: point1)
            path5.addLine(to: point2)
            path5.move(to: point3)
            path5.addLine(to: thirdPoint)
            context.setStrokeColor(UIColor.black.cgColor)
            context.addPath(path5)
            context.strokePath()
            //写上字
            let firstPrice = "¥" + String(prices[0])
            drawText(text: firstPrice, basePoint: firstPoint)
            let secondPrice = "¥" + String(prices[1])
            drawText(text: secondPrice, basePoint: secondPoint)
            let thirdPrice = "¥" + String(prices[2])
            drawText(text: thirdPrice, basePoint: thirdPoint)
            
            //写上月份
            if months.count > 0 {
                drawMonth(text: months[0], basePoint: firstPoint)
                drawMonth(text: months[1], basePoint: secondPoint)
                drawMonth(text: months[2], basePoint: thirdPoint)
            }
        }
    }
    
    func drawMonth(text : String,basePoint : CGPoint) -> Void {
            let attribute = [NSAttributedStringKey.foregroundColor : UIColor.colorWithHexString(hex: "2c2c2c"),NSAttributedStringKey.font : UIFont.boldSystemFont(ofSize: 24 * YXHeightScale)]
            let target = NSString.init(string: text)
            let size = text.StringSize(font: UIFont.systemFont(ofSize: 24 * YXWidthScale))
            target.draw(at: CGPoint.init(x: basePoint.x - size.width / 2, y: self.frame.size.height - size.height), withAttributes: attribute)
    }
    
    func drawText(text : String,basePoint : CGPoint) -> Void {
        let attribute = [NSAttributedStringKey.foregroundColor : UIColor.colorWithHexString(hex: "3cc282"),NSAttributedStringKey.font : UIFont.boldSystemFont(ofSize: 24 * YXHeightScale)]
        let target = NSString.init(string: text)
        let size = text.StringSize(font: UIFont.systemFont(ofSize: 24 * YXWidthScale))
        let point : CGPoint = CGPoint.init(x: basePoint.x, y: basePoint.y - size.height)
        target.draw(at: point, withAttributes: attribute)
    }
}
